<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="巡更路线名称" prop="prName">
        <el-input
          v-model="queryParams.prName"
          placeholder="请输入巡更路线名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="巡更点数量" prop="prAmount">
        <el-input
          v-model="queryParams.prAmount"
          placeholder="请输入巡更点数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="巡更点位" prop="prKpId">
        <el-input
          v-model="queryParams.prKpId"
          placeholder="请输入巡更点位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['electronicPatrolling:route:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['electronicPatrolling:route:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['electronicPatrolling:route:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['electronicPatrolling:route:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="routeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="${comment}" align="center" prop="prId" />
      <el-table-column label="巡更路线名称" align="center" prop="prName" />
      <el-table-column label="巡更点数量" align="center" prop="prAmount" />
      <el-table-column label="巡更点位" align="center" prop="prKpId" />
      <el-table-column label="备注" align="center" prop="ptRemarks" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['electronicPatrolling:route:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['electronicPatrolling:route:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改巡更路线对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="巡更路线名称" prop="prName">
          <el-input v-model="form.prName" placeholder="请输入巡更路线名称" />
        </el-form-item>
        <el-form-item label="巡更点数量" prop="prAmount">
          <el-input v-model="form.prAmount" placeholder="请输入巡更点数量" />
        </el-form-item>
        <el-form-item label="巡更点位" prop="prKpId">
          <el-input v-model="form.prKpId" placeholder="请输入巡更点位" />
        </el-form-item>
        <el-form-item label="备注" prop="ptRemarks">
          <el-input v-model="form.ptRemarks" placeholder="请输入备注" />
        </el-form-item>
        <el-divider content-position="center">巡更点配置信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddTbKeepWatch">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteTbKeepWatch">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="tbKeepWatchList" :row-class-name="rowTbKeepWatchIndex" @selection-change="handleTbKeepWatchSelectionChange" ref="tbKeepWatch">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="巡更点名称" prop="kwName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.kwName" placeholder="请输入巡更点名称" />
            </template>
          </el-table-column>
          <el-table-column label="巡更点编号" prop="kwNumber" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.kwNumber" placeholder="请输入巡更点编号" />
            </template>
          </el-table-column>
          <el-table-column label="楼栋楼层" prop="kwLower" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.kwLower" placeholder="请输入楼栋楼层" />
            </template>
          </el-table-column>
          <el-table-column label="巡更二维码" prop="kwImg" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.kwImg" placeholder="请输入巡更二维码" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRoute, getRoute, delRoute, addRoute, updateRoute } from "@/api/electronicPatrolling/route";

export default {
  name: "Route",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedTbKeepWatch: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 巡更路线表格数据
      routeList: [],
      // 巡更点配置表格数据
      tbKeepWatchList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        prName: null,
        prAmount: null,
        prKpId: null,
        ptRemarks: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询巡更路线列表 */
    getList() {
      this.loading = true;
      listRoute(this.queryParams).then(response => {
        this.routeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        prId: null,
        prName: null,
        prAmount: null,
        prKpId: null,
        ptRemarks: null
      };
      this.tbKeepWatchList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.prId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加巡更路线";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const prId = row.prId || this.ids
      getRoute(prId).then(response => {
        this.form = response.data;
        this.tbKeepWatchList = response.data.tbKeepWatchList;
        this.open = true;
        this.title = "修改巡更路线";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.tbKeepWatchList = this.tbKeepWatchList;
          if (this.form.prId != null) {
            updateRoute(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRoute(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const prIds = row.prId || this.ids;
      this.$modal.confirm('是否确认删除巡更路线编号为"' + prIds + '"的数据项？').then(function() {
        return delRoute(prIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 巡更点配置序号 */
    rowTbKeepWatchIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 巡更点配置添加按钮操作 */
    handleAddTbKeepWatch() {
      let obj = {};
      obj.kwName = "";
      obj.kwNumber = "";
      obj.kwLower = "";
      obj.kwImg = "";
      this.tbKeepWatchList.push(obj);
    },
    /** 巡更点配置删除按钮操作 */
    handleDeleteTbKeepWatch() {
      if (this.checkedTbKeepWatch.length == 0) {
        this.$modal.msgError("请先选择要删除的巡更点配置数据");
      } else {
        const tbKeepWatchList = this.tbKeepWatchList;
        const checkedTbKeepWatch = this.checkedTbKeepWatch;
        this.tbKeepWatchList = tbKeepWatchList.filter(function(item) {
          return checkedTbKeepWatch.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleTbKeepWatchSelectionChange(selection) {
      this.checkedTbKeepWatch = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('electronicPatrolling/route/export', {
        ...this.queryParams
      }, `route_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
